package cn.z2huo.demo.mybatis.plus.service.impl;

import cn.z2huo.core.enums.YesNoEnum;
import cn.z2huo.demo.model.dataobject.role.RoleDO;
import cn.z2huo.demo.model.dataobject.user.UserDO;
import cn.z2huo.demo.model.dataobject.user.UserRoleRelationDO;
import cn.z2huo.demo.model.po.user.UserRoleRelationPO;
import cn.z2huo.demo.mybatis.plus.mapper.CustomUserRoleRelationMapper;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>联接表测试
 * <p>mybatis-plus-join插件联接表
 */
@Slf4j
@Service
public class MybatisPlusJoinJoinServiceImpl {

    @Autowired
    private CustomUserRoleRelationMapper customUserRoleRelationMapper;

    public void innerJoin() {
        // MPJLambdaWrapper类的泛型必须是主表类型, 并且要用主表对应的Mapper调用
        MPJLambdaWrapper<UserRoleRelationDO> joinWrapper = JoinWrappers.lambda(UserRoleRelationDO.class);
        joinWrapper.selectAll(UserRoleRelationDO.class)
                .select(RoleDO::getRoleName)
                .select(UserDO::getUserName)
                .innerJoin(RoleDO.class, RoleDO::getId, UserRoleRelationDO::getRoleId)
                .innerJoin(UserDO.class, UserDO::getUserCode, UserRoleRelationDO::getUserCode)
                .eq(UserDO::getValidFlag, YesNoEnum.YES.getValue())
                .eq(UserDO::getDeleteFlag, YesNoEnum.NO.getValue())
                .eq(RoleDO::getValidFlag, YesNoEnum.YES.getValue())
                .eq(RoleDO::getDeleteFlag, YesNoEnum.NO.getValue());
        List<UserRoleRelationPO> userRoleRelationPOS = customUserRoleRelationMapper.selectJoinList(UserRoleRelationPO.class, joinWrapper);
        log.info("mybatis-plus-join, innerJoin, userRoleRelationPOS:{}", userRoleRelationPOS);
    }

}
